import { localCache } from '@/utils/cache'
import { BASE_URL, TIME_OUT } from './config'
import HYRequest from './request'
import { LOGIN_TOKEN } from '@/global/constants'

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn(config) {
      const token = localCache.getCache(LOGIN_TOKEN)?.replace(/^"|"$/g, ''); // 去掉首尾的双引号
      if (config.headers && token) {  // 类型缩小,判断是否有值
        config.headers.Authorization = 'Bearer ' + token
      }
      return config
    },
  }
})

export default hyRequest
